package com.hyc.util;

import cn.hutool.core.util.StrUtil;
import cn.hutool.setting.dialect.Props;

/**
 * @author 冷环渊
 * @date 2025/6/18 16:32
 * @description ConfigLoader
 */
public class ConfigLoader {
    public static <T> T loadConfig(Class<T> targetClass, String prefix) {
        return loadConfig(targetClass, prefix, "");
    }

    private static <T> T loadConfig(Class<T> targetClass, String prefix, String environment) {
        StringBuilder configFileNameBuilder = new StringBuilder("application");
        if (StrUtil.isNotBlank(environment)) {
            configFileNameBuilder.append("-").append(environment);
        }
        configFileNameBuilder.append(".properties");
        //加载配置文件
        Props props = new Props(configFileNameBuilder.toString());
        if (props.isEmpty()) {
            System.out.println("加载配置失败" + configFileNameBuilder.toString());
        } else {
            System.out.println("加载配置成功" + props);
        }
        return props.toBean(targetClass, prefix);
    }
}
